package com.yanqu.road.dao.impl.activity.tanghulu;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tanghulu.TangHuLuMakeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TangHuLuMakeDataDaoImpl extends TempDataObjectDao<TangHuLuMakeData> {


    public static String getTangHuLuMakeDataSql = "select * from t_u_activity_tanghulu_round where activityId=? and userId=? order by roundNo";
    public List<TangHuLuMakeData> getTangHuLuMakeData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<TangHuLuMakeData> result = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getTangHuLuMakeDataSql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setLong(i++,userId);
            rs = call.executeQuery();
            while (rs.next()) {
                TangHuLuMakeData data = parse(rs);
                result.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }


    public static String insertTangHuLuMakeDataSql = "insert into t_u_activity_tanghulu_round(activityId,userId,roundNo,type,score,turnScore) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(TangHuLuMakeData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoundNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isTurnScore()));
        if (getDb().executeNoneQuery(insertTangHuLuMakeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateTangHuLuMakeDataSql = "update t_u_activity_tanghulu_round set type=?,score=?,turnScore=? where activityId=? and userId=? and roundNo=?";
    @Override
    protected boolean updateData(TangHuLuMakeData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isTurnScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoundNo()));
        if (getDb().executeNoneQuery(updateTangHuLuMakeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected TangHuLuMakeData parseData(ResultSet rs) throws SQLException {
        TangHuLuMakeData tangHuLuMakeData = new TangHuLuMakeData();
        tangHuLuMakeData.setActivityId(rs.getInt("activityId"));
        tangHuLuMakeData.setUserId(rs.getLong("userId"));
        tangHuLuMakeData.setRoundNo(rs.getInt("roundNo"));
        tangHuLuMakeData.setType(rs.getInt("type"));
        tangHuLuMakeData.setScore(rs.getLong("score"));
        tangHuLuMakeData.setTurnScore(rs.getBoolean("turnScore"));
        return tangHuLuMakeData;
    }
}
